<?php
/**
 * Created by PhpStorm.
 * User: chang
 * Date: 2019/4/1
 * Time: 11:54
 */

namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class UserLoginLog extends Model
{
    protected $table = 'user_login_log';
    protected $redisOsVersionMap = 'CACHE:OS_VERSION';
    public $timestamps = false;
    
    /**
     * 创建日志
     * @param int $user_id
     * @param string $version_no
     * @param string $channel_id
     * @param string $device
     * @param string $os_name
     * @return bool
     */
    public function createdLoginLog(int $user_id, string $version_no, string $channel_id, string $device, string $os_name ,string  $ip, string $guid){
        $model = new UserLoginLog;
        $model->user_id = $user_id;
        $model->version_no = $version_no;
        $model->channel_id = $channel_id;
        $model->device = $device;
        $model->os_name = $os_name;
        $model->time = $_SERVER['REQUEST_TIME'];
        $model->ip = $ip;
        $model->guid = $guid;
        $res = $model->save();
        return $res;
    }

    /**获取用户最后一次登录app的ip
     * @param $user_id
     * @return int
     */
    public function getLoginLog($user_id){
        $res = DB::table($this->table)
            ->select('ip')
            ->where('user_id' ,$user_id)
            ->orderBy('time' ,'desc')
            ->first();
        return !empty($res) ? $res->ip : 0;
    }

    /**
     * 获取用户最后一次登录app的信息
     * @param $user_id
     * @return array
     */
    public function getLoginLogInfo($user_id){
        $res = DB::table($this->table)
            ->where('user_id' ,$user_id)
            ->orderBy('id' ,'desc')
            ->first();
        return $res ? (array)$res : [];
    }

    /**
     * 设置用户跟版本号的映射
     *
     * @Author Jerry handanyingjie@sina.com
     * @DateTime 2021-08-31 12:01:54
     * @param integer $userId
     * @param string $versionNo
     * @return integer
     */
    public function setOsVersion(int $userId = 0, string $versionNo = ''):int {
        return (int) Redis::HSET($this->redisOsVersionMap, $userId, $versionNo);
    }

    /**
     * 获取用户版本号
     *
     * @Author Jerry handanyingjie@sina.com
     * @DateTime 2021-08-31 12:02:13
     * @param integer $userId
     * @return string
     */
    public function getOsVersion(int $userId = 0):string {
        return (string) Redis::HGET($this->redisOsVersionMap, $userId);
    }


    /**
     * 获取用户登录设备数量
     *
     * @Author zxiaofee
     * @DateTime 2022-09-17 18:02:13
     * @param integer $userId
     * @return string
     */
    public function getDeviceNumByUserId(int $userId = 0):string {
        return DB::table($this->table)->select('guid')->distinct()
            ->where('user_id' ,$userId)
            ->count();

    }
    public function getDeviceNumByUserId2(int $userId = 0):string {
        $count = "SELECT count(DISTINCT guid) as a from zb_user_login_log  WHERE user_id = $userId";
        $query=DB::select($count);
        $array = json_decode(json_encode($query),true);
        return $array['0']['a'];
    }
    /**
     * 与zb_user表建立关联关系
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'user_id');
    }
}